﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Extensions;

namespace AAA.Google
{
    //用法GoogleNotifier abc=new GoogleNotifier();
    //abc.SendSMSMsg(string TheMessage)
    //功能 一分鐘&兩分鐘後各通知一次
    public class GoogleNotifier
    {
        private CalendarService _service;
        private CalendarEntry _calendarEntry;

        private const string CALENDAR_URL = "http://www.google.com/calendar/feeds/default/allcalendars/full";

        private string _strUsername;

        public string Username
        {
            get { return _strUsername; }
            set { _strUsername = value; }
        }

        private string _strPassword;

        public string Password
        {
            get { return _strPassword; }
            set { _strPassword = value; }
        }

        private string _strCalendarName;

        public string CalendarName
        {
            get { return _strCalendarName; }
            set { _strCalendarName = value; }
        }

        public void InitNotifier()
        {
            //創建日曆的服務對象
            _service = new CalendarService("CalendarTestCommucation");
            //輸入帳號密碼
            _service.setUserCredentials(Username, Password);
            //找到帳戶中"SMSTest"的日曆
            CalendarQuery query = new CalendarQuery(CALENDAR_URL);
            _calendarEntry = null;
            foreach (CalendarEntry ce in _service.Query(query).Entries)
            {
                if (ce.Title.Text == CalendarName) _calendarEntry = ce;
            }
        }
        public void SendSMSMsg(string strTitle, string strMessage, DateTime dtStartTime, DateTime dtEndTime, int iNotifyMinute)
        {
            SendSMSMsg(strTitle, strMessage, dtStartTime, dtEndTime, new int[] { iNotifyMinute});
        }

        public void SendSMSMsg(string strTitle, string strMessage, DateTime dtStartTime, DateTime dtEndTime, int[] iNotifyMinutes)
        {
            EventEntry eventEntry = new EventEntry(strTitle, strMessage);
            //                         開始時間                  結束時間
            eventEntry.Times.Add(new When(dtStartTime, dtEndTime));


            for (int i = 0; i < iNotifyMinutes.Length; i++)
            {
                //N分鐘前通知
                Reminder alarmReminder = new Reminder();
                alarmReminder.Minutes = iNotifyMinutes[i];
                alarmReminder.Method = Reminder.ReminderMethod.sms;

                eventEntry.Reminders.Add(alarmReminder);
            }
            _service.Insert(new Uri(_calendarEntry.Content.AbsoluteUri), eventEntry);

        }

    }
}
